package com.mazaiting.redeye.utils;

import javax.servlet.ServletInputStream;
import java.io.*;
import java.nio.charset.Charset;

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 * @author mazaiting
 * @date 2019-06-20
 * @decription 流工具
 */
public class StreamUtil {

    /**
     * 将输入流转换为字符串
     * @param is 输入流
     * @return 字符串
     */
    public static String getString(InputStream is) {
        // 创建字符串
        StringBuilder sb = new StringBuilder();
        // 用缓冲器读行
        BufferedReader reader = null;
        try {
            // 初始化
            reader = new BufferedReader(new InputStreamReader(is, Charset.defaultCharset()));
            // 行内容
            String line;
            // 循环读取
            while ((line = reader.readLine()) != null) {
                // 拼接内容
                sb.append(line);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            closeStream(reader);
        }

        return sb.toString();
    }

    /**
     * 输出字符串
     * @param is 输入流
     * @return 字符串
     */
    public static String getKeyString(InputStream is) {
        // 创建字符串
        StringBuilder builder = new StringBuilder();
        // 创建缓冲器
        BufferedReader reader = null;
        try {
            // 用一个读输出流类去读
            InputStreamReader isr = new InputStreamReader(is);
            // 用缓冲器读行
            reader = new BufferedReader(isr);
            // 行内容
            String line;
            // 添加换行
            builder.append("\n");
            //直到读完为止
            while ((line = reader.readLine()) != null) {
                // 拼接字符串
                builder.append(line).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeStream(reader);
        }
        return builder.toString();
    }

    /**
     * 关闭流
     * @param closeable 流共有接口
     */
    private static void closeStream(Closeable closeable) {
        try {
            if (null != closeable) {
                closeable.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
